﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Collections;
using System.Windows.Forms;
using Loong.Model;

namespace QYWebSiteBN
{
    public partial class Form_Tag : Form
    {
        private int index=-1;

        public Form_Tag()
        {
            InitializeComponent();

            init();
           
        }

        public Form_Tag(int index):this()
        {
            this.index = index;
            SetTag(index);
        }


        public void SetTag(int index)
        {
            Loong.Model.ContentTagInfo tag = HTMLTag.Get(index);

            txtName.Text=tag.Name;
            txtBeginMathing.Text = tag.BeginMathing;
            txtEndMathing.Text= tag.EndMatching ;
            cbLoopMatching.Checked=tag.LoopMatching;
            cbPageMatching.Checked=tag.PageMatching;
            cbToAbsolutePath.Checked = tag.ToAbsolutePath;
            cbIsDownloadFile.Checked = tag.IsDownloadFile;
            cbIsDownloadFlash.Checked=  tag.IsDownloadFlash;
            cbIsDownloadImg.Checked=tag.IsDownloadImg;
            cbfindRealFile.Checked = tag.FindRealFile;
             txtFileInclude.Text= tag.FileInclude;
             txtFilesaveFormat.Text=tag.FileSaveFormat;
            txtIncludeString.Text= tag.IncludeString ;
            txtNoIncludeString.Text=tag.NoIncludeString;
            cbResultIsNoEmpty.Checked=tag.ResultIsNoEmpty;
            int count = tag.ClearHtmlTag.Count;
            for (int i = 0; i < count; i++)
            {
                int v =tag.ClearHtmlTag.Dequeue();
              lbHtmlTag.SetItemChecked(v, true);       

            }
            count=tag.ClearString.Count;
            lvContentClear.Items.Clear();
            for (int i = 0; i < count; i++)
            {                 
                lvContentClear.Items.Add(tag.ClearString.Dequeue());

            }
            count = tag.ReplaceString.Count;
            lvPlace.Items.Clear();
            for (int i = 0; i < count; i++)
            {
                ListViewItem item = new ListViewItem(tag.ReplaceString.Dequeue());
                lvPlace.Items.Add(item);
            }

               

        }


        /// <summary>
        /// 对组件进行数据初始化
        /// </summary>
        public void init()
        {
            //***=*添加html标签到选项框******/
            ContentTagInfo tag = new ContentTagInfo();
            lbHtmlTag.Items.Clear();
            int l = 24;
            for (int i = 0; i < l;i++ )
            {
                if (!string.IsNullOrEmpty(tag.HTMLTag[i, 0]))
                {
                    lbHtmlTag.Items.Add(tag.HTMLTag[i, 0]);
                }
            }
            //***=*添加html标签-end ******/

            updateContentClassList(-1, "china");
         


        }

        /// <summary>
        /// 标签全选
        /// </summary>
        private void btnTagSelAll_Click(object sender, EventArgs e)
        {
            for(int i=0;i<lbHtmlTag.Items.Count;i++)
            {
                lbHtmlTag.SetItemChecked(i, true);
            }
                         
           
        }

        /// <summary>
        /// 标签反选
        /// </summary>
        private void btnTagDeSel_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < lbHtmlTag.Items.Count; i++)
            {
                lbHtmlTag.SetItemChecked(i, !lbHtmlTag.GetItemChecked(i));                 
            }
        }

        /// <summary>
        /// 确认
        /// </summary>
        private void btnConfirm_Click(object sender, EventArgs e)
        {

            if (string.IsNullOrEmpty(txtName.Text))
            {
                MessageBox.Show("名称不能为空!");
                txtName.Focus();
                return;
            }


            ContentTagInfo tag = new ContentTagInfo();
            tag.Name = txtName.Text;
            tag.BeginMathing = txtBeginMathing.Text;
            tag.EndMatching = txtEndMathing.Text;
            tag.LoopMatching = cbLoopMatching.Checked;
            tag.PageMatching = cbPageMatching.Checked;
            tag.ToAbsolutePath = cbToAbsolutePath.Checked;
            tag.IsDownloadFile = cbIsDownloadFile.Checked;
            tag.IsDownloadFlash = cbIsDownloadFlash.Checked;
            tag.IsDownloadImg = cbIsDownloadImg.Checked;
            tag.FindRealFile = cbfindRealFile.Checked;
            tag.FileInclude = txtFileInclude.Text;
            tag.FileSaveFormat = txtFilesaveFormat.Text;
         
            tag.IncludeString = txtIncludeString.Text;
            tag.NoIncludeString = txtNoIncludeString.Text;
            tag.ResultIsNoEmpty = cbResultIsNoEmpty.Checked;

            
            for(int i=0;i<lbHtmlTag.Items.Count;i++)
            {
                if(lbHtmlTag.GetItemChecked(i))
                {
                    tag.ClearHtmlTag.Enqueue(i);
                }                
            }



         


            Queue<string> clear=new Queue<string>();

            for(int i=0;i<lvContentClear.Items.Count;i++)
            {
                clear.Enqueue(lvContentClear.Items[i].Text);
            }

            tag.ClearString = clear;

            Queue<string[]> replace = new Queue<string[]>();
            for (int i = 0; i < lvPlace.Items.Count; i++)
            {

                string[] item = { lvPlace.Items[i].Text, lvPlace.Items[i].SubItems[1].Text };
                replace.Enqueue(item);
            }

            tag.ReplaceString = replace;

            if (index < 0)
            {
                HTMLTag.Add(tag);
            }
            else
            {
                HTMLTag.Edit(index, tag);
            }
            Form_EditTack ftack = (Form_EditTack)this.Owner;
         
            ftack.UpdateTagList();
            this.Close();

          
           
        }

        /// <summary>
        /// 打开清除内容窗口
        /// </summary>
        private void btnClearContentAdd_Click(object sender, EventArgs e)
        {
            Form_ContentClear fclear = new Form_ContentClear();
            fclear.Owner = this;
            fclear.ShowDialog();
        }

        /// <summary>
        /// 编辑更新排除列表
        /// </summary>
        public void updateContentClassList(int index,string content)
        {
            if (index < 0)
            {
                lvContentClear.Items.Add(content);
            }
            else
            {

                lvContentClear.Items[index].Text = content;
            }
        }

        /// <summary>
        /// 更新内容替换列表
        /// </summary>
        /// <param name="index"></param>
        /// <param name="source"></param>
        /// <param name="obj"></param>
        public void updateContentRepace(int index, string source, string obj)
        {
               ListViewItem item = new ListViewItem(new string[] { source, obj });
            if (index < 0)
            {
             
                lvPlace.Items.Add(item);
          
            }
            else
            {
                lvPlace.Items[index] = item;
            }
 
        }

        /// <summary>
        /// 编辑排除内容
        /// </summary>
        private void lbContentCliare_DoubleClick(object sender, EventArgs e)
        {
            if (lvContentClear.SelectedItems != null)
            {
                Form_ContentClear fclear = new Form_ContentClear(lvContentClear.SelectedItems[0].Index,lvContentClear.SelectedItems[0].Text);
                fclear.Owner = this;
                fclear.ShowDialog();
            }
        }

        /// <summary>
        /// 编辑排除内容
        /// </summary>
        private void btnClearContentEdit_Click(object sender, EventArgs e)
        {
            if (lvContentClear.SelectedItems != null)
            {
                Form_ContentClear fclear = new Form_ContentClear(lvContentClear.SelectedItems[0].Index, lvContentClear.SelectedItems[0].Text);
                fclear.Owner = this;
                fclear.ShowDialog();
            }

        }

        /// <summary>
        /// 添加替换内容
        /// </summary>
        private void btnReplaceAdd_Click(object sender, EventArgs e)
        {
            Form_Content_Replace freplace = new Form_Content_Replace();
            freplace.Owner = this;
            freplace.ShowDialog();
        }


        /// <summary>
        /// 编辑替换内容
        /// </summary>
        private void btnReplaceEdit_Click(object sender, EventArgs e)
        {
            if (lvPlace.SelectedItems != null)
            {
                Form_Content_Replace freplace = new Form_Content_Replace(lvPlace.SelectedItems[0].Index, lvPlace.SelectedItems[0].ListView.Items[0].Text, "");
            }
        }

        /// <summary>
        /// 编辑替换内容
        /// </summary>
         private void lvPlace_DoubleClick(object sender, EventArgs e)
        {
            if (lvPlace.SelectedItems != null)
            {
                Form_Content_Replace freplace = new Form_Content_Replace(lvPlace.SelectedItems[0].Index, lvPlace.SelectedItems[0].Text, lvPlace.SelectedItems[0].SubItems[1].Text);
                freplace.Owner = this;
                freplace.ShowDialog();
            }

        }
        /// <summary>
        ///删除选定的替换内容
        /// </summary>
        private void btnReplaceDelete_Click(object sender, EventArgs e)
        {
            while (this.lvPlace.SelectedItems.Count != 0)
            {
                this.lvPlace.Items.Remove(this.lvPlace.SelectedItems[0]);
            }
        }

        /// <summary>
        /// 删除选定的排除内容
        /// </summary>
        private void btnClearContentDelete_Click(object sender, EventArgs e)
        {
            while (this.lvContentClear.SelectedItems.Count != 0)
            {
                this.lvContentClear.Items.Remove(this.lvContentClear.SelectedItems[0]);
            }
      
        }

        /// <summary>
        /// 向开始字符框添加(*)
        /// </summary>
        private void linkLabel3_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            txtBeginMathing.AppendText("(*)");
        }

        /// <summary>
        /// 向结束字符框添加(*)
        /// </summary>
        private void linkLabel4_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            txtEndMathing.AppendText("(*)");
        }

        /// <summary>
        /// 关闭窗口
        /// </summary>
        private void button8_Click(object sender, EventArgs e)
        {
            this.Close();
        }

    }


}
